package model

import "errors"

// WaveStatus 波次状态
type WaveStatus int

const (
	WaveStatusPending   WaveStatus = iota // 待处理
	WaveStatusCreated   WaveStatus = 1    // 已创建
	WaveStatusReleased  WaveStatus = 2    // 已释放
	WaveStatusPicking   WaveStatus = 3    // 拣货中
	WaveStatusPacked    WaveStatus = 4    // 已打包
	WaveStatusLoading   WaveStatus = 5    // 装车中
	WaveStatusInTransit WaveStatus = 6    // 运输中
	WaveStatusDelivered WaveStatus = 7    // 已送达
	WaveStatusCompleted WaveStatus = 8    // 已完成
	WaveStatusCancelled WaveStatus = 9    // 已取消
)

// String 返回波次状态的字符串表示
func (s WaveStatus) String() string {
	switch s {
	case WaveStatusPending:
		return "待处理"
	case WaveStatusCreated:
		return "已创建"
	case WaveStatusReleased:
		return "已释放"
	case WaveStatusPicking:
		return "拣货中"
	case WaveStatusPacked:
		return "已打包"
	case WaveStatusLoading:
		return "装车中"
	case WaveStatusInTransit:
		return "运输中"
	case WaveStatusDelivered:
		return "已送达"
	case WaveStatusCompleted:
		return "已完成"
	case WaveStatusCancelled:
		return "已取消"
	default:
		return "未知状态"
	}
}

// MarshalJSON 实现JSON序列化
func (s WaveStatus) MarshalJSON() ([]byte, error) {
	return []byte(`"` + s.String() + `"`), nil
}

// UnmarshalJSON 实现JSON反序列化
func (s *WaveStatus) UnmarshalJSON(data []byte) error {
	// 移除引号
	str := string(data)
	if len(str) < 2 {
		return errors.New("invalid wave status")
	}
	str = str[1 : len(str)-1]

	// 根据字符串设置状态
	switch str {
	case "待处理":
		*s = WaveStatusPending
	case "已创建":
		*s = WaveStatusCreated
	case "已释放":
		*s = WaveStatusReleased
	case "拣货中":
		*s = WaveStatusPicking
	case "已打包":
		*s = WaveStatusPacked
	case "装车中":
		*s = WaveStatusLoading
	case "运输中":
		*s = WaveStatusInTransit
	case "已送达":
		*s = WaveStatusDelivered
	case "已完成":
		*s = WaveStatusCompleted
	case "已取消":
		*s = WaveStatusCancelled
	default:
		return errors.New("invalid wave status")
	}
	return nil
}
